#include "chat_only.hpp"

Chat_Only::Chat_Only(string name,const char* ip_server,unsigned int port_server,unsigned int port_communication,string password):door_cmd(ip_server,port_server,port_communication,password)
{
  p_file=fopen(name.c_str(),"r");
  fseek(p_file,0,SEEK_END);
}

/*
void read(FILE* f, List_Key* list)
{
  int cara(42);
  bool begin_lign(true);
  bool key_word(true);
  string key("");

  while(cara != EOF)
    {
      if(begin_lign)
	{
	  for(int c=0 ; c < 7 ; ++c)
	    cara=fgetc(f);
	  begin_lign=false;
	}

      if((char)cara == ':')
	{
	  key_word=false;
	  add_key(list,key);
	  key="";
	}

      if(key_word)
	key+=(char)cara;

      if((char)cara == '\n')
	{
	  begin_lign=true;
	  key_word=true;
	  key="";
	}

      cara=fgetc(f);
    }
}
*/

void Chat_Only::run()
{
  int cara(42);
  bool begin_lign(true);
  bool key_word(true);
  bool command(false);
  string key("");
  string cmd("");

  while(true)
    {
      if(begin_lign)
	{
	  for(int c=0 ; c < 7 ; ++c)
	    cara=fgetc(p_file);
	  begin_lign=false;
	}

      if((char)cara == ':')
	{
	  key_word=false;

	  if(key.compare("say"))
	    command=true;
	  key="";
	}

      if(key_word)
	key+=(char)cara;

      if(command)
	cmd+=(char)cara;

      if((char)cara == '\n')
	{
	  if(command)
	    {
	      cout << cmd << endl;
	      door_cmd.send_bigtext(cmd);
	      sleep(1);
	      door_cmd.send_bigtext("TROLOLOLO");
	    }

	  command=false;
	  begin_lign=true;
	  key_word=true;
	  key="";
	  cmd="";
	}

      cara=fgetc(p_file);
    }
}

/*
int main(void)
{
  string name="/home/sagara/.q3a/q3ut4/games.log";
  const char* ip="127.0.0.1";
  string pass="test";

  Chat_Only c(name,ip,34444,44444,pass);

  c.run();

  return 0;
}
*/
